package com.mamahao.vmnetframework.framework.viewmodel

import android.arch.lifecycle.*
import android.util.Log
import com.mamahao.vmnetframework.framework.event.BaseActionEvent
import com.mamahao.vmnetframework.framework.net.datasource.ISimpleDataSource
import com.mamahao.vmnetframework.framework.net.datasource.impl.SimpleSourceImpl
import kotlinx.coroutines.*
import kotlin.coroutines.CoroutineContext

open class BaseViewModel : ViewModel(), LifecycleObserver{

    val dataSource: ISimpleDataSource by lazy {
        SimpleSourceImpl()
    }

    open val mActionLiveData: MutableLiveData<BaseActionEvent> by lazy {
        MutableLiveData<BaseActionEvent>()
    }

    var lifecycleOwner: LifecycleOwner? = null

    fun startLoading() {
        startLoading(null)
    }

    fun startLoading(message: String?) {
        mActionLiveData.value = BaseActionEvent(message, BaseActionEvent.SHOW_LOADING_DIALOG)
    }

    fun dismissLoading() {
        mActionLiveData.value = BaseActionEvent(null, BaseActionEvent.DISMISS_LOADING_DIALOG)
    }

    fun showToast(message: String?) {
        mActionLiveData.value = BaseActionEvent(message, BaseActionEvent.SHOW_TOAST)
    }

    fun finish() {
        mActionLiveData.value = BaseActionEvent(null, BaseActionEvent.FINISH)
    }

    fun finishWithResultOk() {
        mActionLiveData.value = BaseActionEvent(null, BaseActionEvent.FINISH_WITH_RESULT_OK)
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
    fun onDestory() {
        dataSource?.let {
            it.onDestory()
        }
    }

}